<?php

namespace App\Http\Controllers\Api\V1;

use App\Models\Product;
use App\Repository\BannerRepository;
use App\Transformer\BannerTransformer;
use Dingo\Api\Exception\StoreResourceFailedException;

/**
 * Class BannerController
 * @package App\Http\Controllers\Api\V1
 */
class BannerController extends BaseController
{
    /**
     * @var BannerRepository
     */
    public $banner;

    /**
     * BannerController constructor.
     * @param $banner
     */
    public function __construct(BannerRepository $banner)
    {
        $this->banner = $banner;
    }

    /**
     * @param $id
     * @return \Dingo\Api\Http\Response
     */
    public function getBanner($id)
    {
        $rules = [
            'id' => ['required', 'min:0', 'numeric', 'integer'],
        ];
        $id = ['id' => $id];

        $validator = app('validator')->make($id, $rules);

        if ($validator->fails()) {
            throw new StoreResourceFailedException('ID必须是正整数', $validator->errors());
        }

        $banner = ($this->banner->getBanner($id));

        return $this->response->item($banner, new BannerTransformer());
    }
}
